home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI803.ASC < prev    next >
Text File  |  1992-02-25  |  2KB  |  133 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  C++                                    NUMBER  :  803
  9.   VERSION  :  All
  10.        OS  :  PC DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/1
  12.  
  13.     TITLE  :  Dynamically Allocated Multidimensional Arrays
  14.  
  15.  
  16.  
  17.  
  18.   #include <stdio.h>
  19.   #include <alloc.h>
  20.  
  21.   #define     ROW         10
  22.   #define     COLUMN      15
  23.  
  24.   typedef int ARRAYTYPE;
  25.   typedef ARRAYTYPE * ARRAYTYPEPTR;
  26.  
  27.      /*  Declare the 2D array as a pointer to a pointer to
  28.   ARRAYTYPE  */
  29.      /*  Note there is no memory allocated for the array yet.
  30.   */
  31.  
  32.   ARRAYTYPEPTR *array;
  33.  
  34.   void main()
  35.   {
  36.       int i, j;
  37.  
  38.       /*  First, allocate an array of pointers to ARRAYTYPE of ROW
  39.   */
  40.       /*  elements.  Next, looping through the array of pointers,
  41.   */
  42.       /*  allocate arrays of ARRAYTYPE of COLUMN elements and
  43.   assign   */
  44.       /*  them to the pointers.  This assigns the necessary memory
  45.   to  */
  46.       /*  the array, which you can then index via the [] operators.
  47.   */
  48.  
  49.       array = (ARRAYTYPEPTR *) malloc (ROW * sizeof(ARRAYTYPEPTR));
  50.       for (i = 0; i < ROW; i++)
  51.           array[i] = (ARRAYTYPEPTR) malloc (COLUMN * sizeof
  52.   (ARRAYTYPE));
  53.  
  54.       /*  Fill array with unique values  */
  55.  
  56.       for (i = 0; i < ROW; i++)
  57.           for (j = 0; j < COLUMN; j++)
  58.               array[i][j] = (COLUMN * i) + j;
  59.  
  60.       /*  Print out array to make sure everything's OK  */
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.   PRODUCT  :  C++                                    NUMBER  :  803
  76.   VERSION  :  All
  77.        OS  :  PC DOS
  78.      DATE  :  February 25, 1992                        PAGE  :  2/1
  79.     TITLE  :  Dynamically Allocated Multidimensional Arrays
  80.  
  81.  
  82.  
  83.  
  84.       for (i = 0; i < ROW; i++)    {
  85.           for (j = 0; j < COLUMN; j++)
  86.               printf("%4d",array[i][j]);
  87.           putchar('\n');
  88.       }
  89.   }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.